package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.NewYddUserDto;
import com.itheima.ydd.dto.SysUserDto;
import com.itheima.ydd.dto.ArticleDto;
import com.itheima.ydd.dto.YddCateDto;
import com.itheima.ydd.entity.*;
import com.itheima.ydd.mapper.SysUserMapper;
import com.itheima.ydd.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysMenuService sysMenuService;

    @Autowired
    private ISysDictService sysDictService;

    @Autowired
    private IYddCateService yddCateService;

    @Autowired
    private IYddArticleService yddArticleService;

    @Autowired
    private IYddAdvposService yddAdvposService;

    @Autowired
    private IYddTagService yddTagService;

    @Autowired
    private IYddAdvimgService yddAdvimgService;

    @Autowired
    private IYddUserService yddUserService;

    @Autowired
    private IYddUserDataService yddUserDataService;


    @Override
    public R profile(Long userId) {
        //1.1 查询登录用户信息
        SysUser sysUser = sysUserService.getById(userId);
        //1.2 填入用户角色信息
        LambdaQueryWrapper<SysRole> sysRoleWrapper = new LambdaQueryWrapper<>();
        String sysUserRoleId = sysUser.getRoleId();
        sysRoleWrapper.eq(SysRole::getId,sysUserRoleId.substring(2, sysUserRoleId.length() - 2));
//        sysRoleWrapper.select(SysRole::getName);
        //1.2.1 查出对应的角色详细信息
        SysRole sysRole = sysRoleService.getOne(sysRoleWrapper);
        //1.2.2 封装进sysUserDto对象中
        SysUserDto userInfo = new SysUserDto();
        BeanUtils.copyProperties(sysUser,userInfo);
        userInfo.setRolename(Collections.singletonList(sysRole.getName()));

        //2 查菜单数据
        LambdaQueryWrapper<SysMenu> sysMenuWrapper = new LambdaQueryWrapper<>();
        sysMenuWrapper.ne(SysMenu::getPath,"");
        List<SysMenu> menuList = sysMenuService.list(sysMenuWrapper);

        //3 dictsList
        LambdaQueryWrapper<SysDict> sysDictWrapper = new LambdaQueryWrapper<>();
        sysDictWrapper.select(SysDict::getType);
        sysDictWrapper.groupBy(SysDict::getType);
        //3.1 分组查询所有dict_type
        List<Object> dictTypes = sysDictService.listObjs(sysDictWrapper);
        Map<String, Map<String,String>> dictsList = new HashMap<>();
        //3.2 遍历dictTypes ，查询对应的dict信息，存入dictsList中
        for (Object o : dictTypes) {
            Map<String, String> map = new HashMap<>();

            sysDictWrapper = new LambdaQueryWrapper<>();
            sysDictWrapper.eq(SysDict::getType,o);
            List<SysDict> sysDicts = sysDictService.list(sysDictWrapper);
            for (SysDict sysDict : sysDicts) {
                //3.3 将dict_value,dict_label存入集合
                map.put(sysDict.getValue(),sysDict.getLabel());
            }

            //3.4 将保存后的数据存入dictsList
            dictsList.put((String)o,map);
        }

        //4 准备 btnList
        Map<Integer,String> btnList = new HashMap<>();
        btnList.put(0,"*");

        //5 将所有查出的数据存入 R 中，返回
        R r = new R();
        r.putData("btnList",btnList);
        r.putData("dictsList",dictsList);
        r.putData("menuList",menuList);
        r.putData("userInfo",userInfo);

        r.setErrno(0);
        r.setErrmsg("");
        return r;
    }

    @Override
    public R baseInfo() {
        //查询所有文章分类类型
        List<YddCate> list = yddCateService.list();
        List<YddCateDto> all_cate=new ArrayList<>();
        list.forEach(cate->{
            YddCateDto yddCateDto=new YddCateDto();
            //设置分类id
            yddCateDto.setId(cate.getId());
            //设置分类名称
            yddCateDto.setCatename(cate.getCatename());
            //获取分类下的文章数量
            QueryWrapper<YddArticle> lqw=new QueryWrapper<>();
            lqw.eq("cateid",cate.getId());
            Integer count = yddArticleService.count(lqw);
            //设置分类下的文章数量
            yddCateDto.setNum(count);
            all_cate.add(yddCateDto);
        });

        //查询广告位总数
        Integer advpos_num = yddAdvposService.count();

        //查询标签总数
        Integer tag_num = yddTagService.count();

        //查询管理员总数
        Integer admin_num = sysUserService.count();

        //查询文章总数
        Integer article_num = yddArticleService.count();

        //查询分类总数
        Integer cate_num=yddCateService.count();

        //查询广告图总数
        Integer advimg_num =yddAdvimgService.count();

        //查询最新用户数据
        LambdaQueryWrapper<YddUser> lqw=new LambdaQueryWrapper<>();
        lqw.isNull(YddUser::getDefaultData);
        List<YddUser> users = yddUserService.list(lqw);
        List<NewYddUserDto> new_user=new ArrayList<>();
        for (YddUser user : users) {
            NewYddUserDto newYddUserDto= new NewYddUserDto();
            newYddUserDto.setId(user.getId());
            newYddUserDto.setUsername(user.getUsername());
            newYddUserDto.setSex(user.getSex());
            newYddUserDto.setIcon(user.getIcon());
            newYddUserDto.setAddress(user.getAddress());
            new_user.add(newYddUserDto);
        }

        //查询热门文章数据
        LambdaQueryWrapper<YddArticle> articleLambdaQueryWrapper=new LambdaQueryWrapper<>();
        articleLambdaQueryWrapper.eq(YddArticle::getIshot,1);
        List<YddArticle> articles = yddArticleService.list(articleLambdaQueryWrapper);
        List<ArticleDto> hot_article = new ArrayList<>();
        for (YddArticle article : articles) {
            ArticleDto articleDto=new ArticleDto();
            articleDto.setId(article.getId());
            articleDto.setTitle(article.getTitle());
            articleDto.setAuthor(article.getAuthor());
            articleDto.setKeywords(article.getKeywords());
            articleDto.setClick(article.getClick());
            hot_article.add(articleDto);
        }

        //查询用户的统计数据
        Map<String,Object> map=new HashMap<>();
        map.put("total",yddUserService.count());
        QueryWrapper<YddUser> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("sex",0);
        map.put("user_man",yddUserService.count(userQueryWrapper));
        QueryWrapper<YddUser> userQueryWrapper2=new QueryWrapper<>();
        userQueryWrapper2.eq("sex",1);
        map.put("user_woman",yddUserService.count(userQueryWrapper2));


        //将数据存入R中返回
        R r=new R();
        r.putData("all_cate",all_cate);
        r.putData("advpos_num",advpos_num);
        r.putData("tag_num",tag_num);
        r.putData("admin_num",admin_num);
        r.putData("article_num",article_num);
        r.putData("cate_num",cate_num);
        r.putData("advimg_num",advimg_num);
        r.putData("new_user",new_user);
        r.putData("hot_article",hot_article);
        r.putData("user",map);

        r.setErrno(0);
        r.setErrmsg("");
        return r;
    }
}
